<template>
  <div class="page-content">
    <ft-table
      :totalCount="totalCount"
      :listData="pageListData"
      v-bind="contentConfig"
      v-model:page="pageInfo"
    >
      <template #headerHandler>
        <el-button
          v-if="showHeader"
          type="primary"
          size="medium"
          @click="handleNewData"
        >
          {{ contentConfig.newBtnTitle ?? 'create new data' }}
        </el-button>
      </template>

      <template #status="scope">
        <el-button
          :type="scope.row.isEnabled ? 'success' : 'danger'"
          size="mini"
          plain
          @click="changeStatus(scope.row)"
        >
          {{ $filters.showStatus(scope.row.isEnabled) }}
        </el-button>
      </template>
      <template #limit="scope">
        <el-button
          :type="scope.row.isLimit ? 'danger' : 'success'"
          size="mini"
          plain
          @click="changeLimit(scope.row)"
        >
          {{ $filters.showLimit(scope.row.isLimit) }}
        </el-button>
      </template>
      <template #create="scope">
        {{ $filters.formatTime(scope.row.createTime) }}
      </template>
      <template #update="scope">
        {{
          scope.row.updateTime === null
            ? 'no'
            : $filters.formatTime(scope.row.updateTime)
        }}
      </template>
      <template #handler="scope">
        <div class="handler">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="handleEditClick(scope.row)"
          >
            edit
          </el-button>
          <el-button
            v-if="isDelete"
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click="handleDeleteClick(scope.row)"
          >
            delete
          </el-button>
        </div>
      </template>
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
      <!-- <template #imageSlot="scope">
        <slot name="imageSlot" :row="scope.row"></slot>
      </template> -->
    </ft-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch, computed } from 'vue'
import { useStore } from '@/store'
import { usePermission } from '@/hooks/usePermission'

import FtTable from '@/base-ui/table'

export default defineComponent({
  components: {
    FtTable
  },
  props: {
    contentConfig: {
      type: Object,
      required: true
    },
    pageName: {
      type: String,
      required: true
    }
  },
  emits: ['newBtnClick', 'editBtnClick'],
  setup(props, { emit }) {
    // 7.按钮是否显示
    const isCreate = usePermission(props.pageName, 'create')
    const isDelete = usePermission(props.pageName, 'delete')
    const isUpdate = usePermission(props.pageName, 'update')
    // const isQuery = usePermission(props.pageName, 'query')
    const showHeader = props.contentConfig.showHeader ?? false
    // 1.请求页面数据
    const store = useStore()

    // 0.绑定pageInfo
    const pageInfo = ref({
      currentPage: 1,
      pageSize: 10
    })
    watch(pageInfo, () => getPageData())

    let otherQueryInfo: any = {}

    // 2.获取数据
    const getPageData = (otherInfo: any = {}) => {
      // if (!isQuery) return 查询权限
      //深copy
      if (JSON.stringify(otherInfo) != '{}') {
        let obj = JSON.stringify(otherInfo)
        otherQueryInfo = JSON.parse(obj)
      }
      if (otherInfo.flag == 'o') {
        otherQueryInfo = {}
      }
      if (otherQueryInfo.isEnabled) {
        let status: number
        if (
          otherQueryInfo.isEnabled == 'active' ||
          otherQueryInfo.isEnabled == '1'
        ) {
          status = 1
        } else {
          status = 0
        }
        //otherQueryInfo.isEnabled === 'active' ? (status = 1) : (status = 0)
        delete otherQueryInfo?.isEnabled
        otherQueryInfo.isEnabled = status
      }
      console.log('otherQueryInfo', otherQueryInfo)
      console.log('otherInfo', otherQueryInfo)

      const email = store.state.email
      store.dispatch('manage/getPageListDataAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          adminEmail: email,
          ...otherQueryInfo
        }
      })
    }
    getPageData()

    // 2.获取页面数据
    const pageListData = computed(() =>
      store.getters['manage/pageListData'](props.pageName)
    )

    // 3.footer
    const totalCount = computed(() =>
      store.getters['manage/pageListDataCount'](props.pageName)
    )

    // 4.剩余需要的插槽
    const otherPropSlots = computed(() => {
      return props.contentConfig.propList.filter((item: any) => {
        if (item.slotName === 'status') return false
        else if (item.slotName === 'create') return false
        else if (item.slotName === 'update') return false
        else if (item.slotName === 'handler') return false
        else if (item.slotName === 'limit') return false
        return true
      })
    })

    const changeStatus = (item: any) => {
      store.dispatch('manage/changeStatusAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...otherQueryInfo
        },
        id: item.userWorkplaceId
      })
    }

    const changeLimit = (item: any) => {
      store.dispatch('manage/changeLimitAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...otherQueryInfo
        },
        id: item.userId
      })
    }

    // 5.删除操作
    const handleDeleteClick = (rowItem: any) => {
      store.dispatch('manage/deletePageDataAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: pageInfo.value.currentPage * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...otherQueryInfo
        },
        id: rowItem.id
      })
    }

    // 6.新建数据
    const handleNewData = () => {
      emit('newBtnClick')
    }
    const handleEditClick = (item: any) => {
      emit('editBtnClick', item)
    }

    return {
      pageInfo,
      pageListData,
      totalCount,
      getPageData,
      otherPropSlots,
      handleDeleteClick,
      changeStatus,
      changeLimit,
      handleEditClick,
      handleNewData,
      isCreate,
      isUpdate,
      isDelete,
      showHeader
    }
  }
})
</script>

<style scoped lang="less">
.page-content {
  border-top: 20px solid #f5f5f5;
  padding: 20px;
}
</style>
